<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends MY_Controller {
	public $data = array();

	public function __construct()
    {
        parent::__construct();
		if($this->session->userdata('login') ){
			$this->data["user_data"] = $this->session->userdata();
		}else{
			redirect("pages/login");
		}
    }
    public function index()
    {

        redirect("main/users");
    }
    public function users(){
        $this->load->model('users_model','u');
        $this->data["navigation"] = "u_index";
        $this->data["title"] = "用户列表";
        $this->data['user'] = $this->u->get_all_user();
        $this->load->view('user',$this->data);
    }
    public function delUser(){
        $id = $this->input->post('id');
        $this->load->model('users_model','u');
        $query = $this->u->delete($id);
        if($query){
            $this->show(1,'删除成功');
        }else{
            $this->show(0,'删除失败');
        }
    }
    public function admin()
    {
        $this->data["navigation"] = "a_index";
        $this->data["title"] = '管理员管理';
        $this->load->model("admin_model");
        $this->data["admin"] = $this->admin_model->get_all_admin();
        $this->load->view('admin',$this->data);
    }

    public function get_by_admin()
    {
        $this->load->model("admin_model");
        $adm_id = $this->input->get('id');
        $query =  $this->admin_model->get_by_id($adm_id);
        echo json_encode($query,true);
    }

    /**
     * 管理员删除
     */
    public function del_admin(){
        $adm_id = $this->input->post('id');
        $this->load->model("admin_model");
        $query = $this->admin_model->delete_admin($adm_id);
        if ($query > 0) {
            $this->show(1, '管理员删除成功');
        } else {
            $this->show(0, '管理员删除失败！');
        }
    }
    /**
     * ajax验证管理员是否重复
     */
    public function urlAjax(){
        $username=$this->input->post('param');
        $this->load->model("admin_model");
        $query =  $this->admin_model->get_by_username($username);
        if($query){
            echo '{"status":"n","info":"已存在"}';
        }else{
            echo '{"status":"y","info":"用户名可用"}';
        }
    }

    public function submit_admin()
    {
        $this->load->model("admin_model");
        $adm_username = $this->input->post("adm_username");
        $adm_realname = $this->input->post("adm_realname");
        $adm_id = $this->input->post('adm_id');
        if($adm_id) {
            $query = $this->admin_model->update($adm_id,$adm_realname);
            if($query){
                $this->show(1,'修改管理员成功',site_url('main/admin'));
            }else{
                $this->show(0,'管理元修改失败');
            }
        }
        $adm_data = array(
            "adm_username" => $adm_username,
            "adm_password" => password_hash('000000',PASSWORD_DEFAULT),
            "adm_role" => 2,
            "adm_realname" 	=> $adm_realname
        );


        $query = $this->admin_model->insert($adm_data);
        if($query){
            $this->show(1,'添加管理员成功',site_url('main/admin'));
        }else{
            $this->show(0,'管理元添加失败');
        }

    }

    public function system(){
        $this->load->model('admin_model','a');
        $this->data['navigation'] = 's_index';
        $this->data['title'] = '系统设置';
        $this->data['system'] = $this->a->get_by_system();
        $this->load->view('system',$this->data);
    }

    /**
     * 配置添加与修改
     */
    public function add_system(){
        $this->load->model('admin_model','a');
        $data = $this->input->post();
        $config['upload_path'] = 'uploads/template';
        $config['allowed_types'] = 'jpg|gif|png';
        $config['file_name'] = rand(1000, 9999).time();
        //加载上传类
        $this->load->library('upload', $config);
        //执行上传任务
        if($this->upload->do_upload('file')){
            $sys_img = $this->a->get_by_system()['sys_logo'];
            $file = $this->upload->data();
            $data['sys_logo'] = $file['file_name'];
        }
        $query = $this->a->update_system($data);
        if ($query > 0) {
            if (isset($sys_img) && is_file('uploads/template/'.$sys_img)) {
                unlink('uploads/template/'.$sys_img);
            }
            redirect('main/system');
        } else{
            show_error('修改失败');
        }
    }

    public function changePassword(){
        $this->data['title'] = '密码修改';
        $this->load->view('password_change',$this->data);
    }

    //change admin passwd
    public function pass_change()
    {
        $old_pass = trim($this->input->post('old_pass'));
        $password = trim($this->input->post('password'));
        $repassword = trim($this->input->post('repassword'));
        if(!$old_pass){
            $this->show(0,'原密码不能为空！');
        }
        if(!$password){
            $this->show(0,'新密码不能为空！');
        }
        if($password!=$repassword){
            $this->show(0,'两次密码不一致，请重新输入！');
        }
        $uid = $this->session->adm_id;
        $query = $this->a->update_passwd($uid,$old_pass,$password);
        if($query==1){
            $this->show(0,'密码输入错误');
        }
        if($query==2){
            $this->show(1,'密码修改成功，请重新登陆');
            $this->redirect('pages/logout');
        }
        show_404('错误');

    }



    public function activities_management($page_no=1)
	{
		$this->data["navigation"] = "m_activity";
				
		$per_page = 50;
		
		$keyword = $this->input->get("keyword");
		
		$this->load->model("activities_model");
		$result = $this->activities_model->search($keyword,$page_no,$per_page);

		$this->data["objects"] = $result["result"];
		$this->data["keyword"] = empty($keyword)?"":$keyword;
		$this->data["total"] = $result["total"];
		
		$this->load->library('pagination');
		
		$config["base_url"] = site_url("main/activity_management");
		$config["total_rows"] = $result["total"];
		$config["use_page_numbers"] = true;
		$config["per_page"] = $per_page;
		$config["reuse_query_string"] = true;
		
		$this->pagination->initialize($config);
		
		$this->load->library('utils');
		
		for($i=0;$i<count($this->data["objects"]);$i++)
		{
			$this->data["objects"][$i]["act_status"] = $this->utils->get_activity_status($this->data["objects"][$i]);
		}
		
		$this->load->view('activities_management' , $this->data);
	}
	

	public function activity($act_id=-1)
	{
		$this->data["navigation"] = "m_activity";
		$this->data["act_id"] = $act_id; 
		$this->load->model("activities_model");
		$this->data["object"] = $this->activities_model->get_by_id($act_id);
		if(empty($this->data["object"]) && $act_id!=-1)show_404();
	
		$this->load->view('activity',$this->data);
	}

	public function submit_activity()
	{
		$act_id = $this->input->post("act_id");
		$act_title = $this->input->post("act_title");
		$act_movie = $this->input->post("act_movie");
		$act_intro = $this->input->post("act_intro");
		$act_date = $this->input->post("act_date");
		$act_start_time = $this->input->post("act_start_time");
		$act_end_time = $this->input->post("act_end_time");
		$act_data_url = $this->input->post("act_data_url");
		$act_bot_freq = $this->input->post("act_bot_freq");
		$act_rules = $this->input->post("act_rules");
		$act_win_count = $this->input->post("act_win_count");
		
		$act_data = array(
			"act_title" => $act_title,
            "act_movie" => $act_movie,
			"act_intro" 	=> $act_intro,
			"act_date" => $act_date,
			"act_start_time" => $act_start_time,
			"act_end_time" => $act_end_time,
			"act_bot_freq" => $act_bot_freq,
			"act_rules" => $act_rules,
			"act_win_count" => $act_win_count,
			"act_data_url" => $act_data_url
		);
		
		$this->load->model("activities_model");
		
		if($act_id == -1){
			$act_id = $this->activities_model->insert($act_data);
		}else{
			$this->activities_model->update($act_id, $act_data);
		}
		
		$config['upload_path']   	= 'upload';
		$config['allowed_types'] 	= 'gif|jpg|png|jpeg';
		$config['file_name'] 			= uniqid();
		$this->load->library('upload', $config);

		if ($this->upload->do_upload('file'))
		{
			$data = $this->upload->data();
			$this->activities_model->update($act_id, array("act_background"=>$data["file_name"]));
		}else{
				//echo $this->upload->display_errors();
		}
		
		$config['file_name'] 			= uniqid();
		$this->load->library('upload', $config);

		if ($this->upload->do_upload('file2'))
		{
			$data = $this->upload->data();
			$this->activities_model->update($act_id, array("act_redpack"=>$data["file_name"]));
		}else{
				//echo $this->upload->display_errors();
		}
		
		$config['file_name'] 			= uniqid();
		$this->load->library('upload', $config);

		if ($this->upload->do_upload('file3'))
		{
			$data = $this->upload->data();
			$this->activities_model->update($act_id, array("act_poster"=>$data["file_name"]));
		}else{
				//echo $this->upload->display_errors();
		}
		
		redirect("main/activities_management");
	}
	
	function delete_activity($act_id=-1)
	{
		$this->load->model("activities_model");
		$this->data["object"] = $this->activities_model->delete($act_id);
		redirect("main/activities_management");
	}
	
	// ADMIN ===========================================
	public function admin_management($page_no=1)
	{
		$this->data["navigation"] = "m_admin";
				
		$per_page = 50;
		
		$keyword = $this->input->get("keyword");
		
		$this->load->model("admin_model");
		$result = $this->admin_model->search($keyword,$page_no,$per_page);
		
		$this->data["objects"] = $result["result"];
		$this->data["keyword"] = empty($keyword)?"":$keyword;
		$this->data["total"] = $result["total"];
		
		
		$this->load->library('pagination');
		
		$config["base_url"] = site_url("main/index");
		$config["total_rows"] = $result["total"];
		$config["use_page_numbers"] = true;
		$config["per_page"] = $per_page;
		$config["reuse_query_string"] = true;
		
		$this->pagination->initialize($config);
		
		$this->load->view('admin_management' , $this->data);
	}
	

	public function show($status,$message,$url='',$info=''){
        $output = array(
            'status'=>$status,
            'message'=>$message,
            'url' => $url,
            'info' => $info
        );

        echo json_encode($output);exit;
    }
		
	public function get_by_code($act_id,$code){
		header('Content-Type: application/json');
		$this->load->model("guesses_model");
		$guesses = $this->guesses_model->get_by_actcode($act_id,$code);
		
		$response = array(
			'code' => 0,
			'message' => '',
			'guesses'=>$guesses
		);
	
		echo json_encode($response);
	}
	
	public function code_validate($gus_id)
	{
		$this->load->model("guesses_model");
		$guesses = $this->guesses_model->update($gus_id,array("gus_code"=>""));
	}

	public function feedback(){
	    $this->data['navigation'] = 'feedback';
        $this->load->model("admin_model");
        $this->data['feedback'] = $this->admin_model->get_feedback();
        $this->load->view('feedback1',$this->data);
    }


}
